package com.yazi.mxz.person.controller.shop;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.myframework.smhj.common.exception.MyException;
import com.myframework.smhj.hibernate.Page;
import com.myframework.smhj.hibernate.Paging;
import com.myframework.smhj.hibernate.Searching;
import com.myframework.smhj.jackson.result.ObjectResult;
import com.myframework.smhj.jackson.result.StatusResult;
import com.yazi.mxz.common.entity.beans.LocalSaleGoods;
import com.yazi.mxz.common.entity.beans.LocalSale;
import com.yazi.mxz.common.entity.beans.Store;
import com.yazi.mxz.common.service.MemberService;
import com.yazi.mxz.common.service.MemberVipService;
import com.yazi.mxz.common.service.LocalSaleService;
import com.yazi.mxz.person.jackson.filter.LocalSaleFilter;

@Controller("shop-localSaleControllerV1.1")
@RequestMapping("/shop/localSale/v1.1")
public class LocalSaleController_V1_1 extends BaseController {
	private static Logger log = LoggerFactory.getLogger(LocalSaleController_V1_1.class);
	
	@Resource
	MemberService memberService;
	@Resource
	LocalSaleService localSaleService;
	@Resource
	MemberVipService memberVipService;
	
	@RequestMapping
	public String index(Model model) {
		debug(log, "线下销售首页");
		model.addAttribute("store", getStore());
		model.addAttribute("member", getMember());
		return "shop/localSale_v1.1";
	}
	
	@RequestMapping("/clientScreen")
	public String clientScreen() {
		log.debug("客户屏页面");
		return "shop/localSale_clientScreen";
	}
	
	@RequestMapping("/initData/json")
	@ResponseBody
	public ObjectResult<?> initDataJson() {
		debug(log, "获取销售的初始数据");
		ObjectResult<InitData> jsonResult = new ObjectResult<InitData>();
		jsonResult.setFilter(LocalSaleFilter.get());
		InitData initData = new InitData();
		Store store;
		LocalSale sale;
		Page<LocalSale> page;
		try {
			
			store = getStore(true);
			
			sale = new LocalSale();
			sale.setStore(store);
			page = localSaleService.list(sale, (Paging)new Searching());
			
			initData.setLocalSales(page.getList());
			initData.setLocalSetting(store.getLocalSetting());
			jsonResult.setResult(initData);
			jsonResult.setOk();
			
		} catch (Exception e) {
			error(log, e, "获取已挂起出售单ID列表异常");
			jsonResult.setErrorMessage("挂起单查询失败，请联系管理员");
		}
		
		return jsonResult;
	}
	
	@RequestMapping("/addDo/json")
	@ResponseBody
	public ObjectResult<Long> addDoJson() {
		debug(log, "添加新销售");
		ObjectResult<Long> jsonResult = new ObjectResult<Long>();
		try {
			
			LocalSale sale = new LocalSale();
			sale.setStore(getStore(true));
			localSaleService.add(sale);
			jsonResult.setResult(sale.getSaleId());
			jsonResult.setOk();
			
		} catch (MyException e) {
			debug(log, "添加新销售失败：" + e.getMessage());
			jsonResult.setFailMessage(e.getMessage());
		} catch (Exception e) {
			error(log, e, "添加新销售异常");
			jsonResult.setErrorMessage("操作失败，请联系管理员");
		}
		
		return jsonResult;
	}
	
	@RequestMapping("/one/json")
	@ResponseBody
	public ObjectResult<?> oneJson(long saleId) {
		debug(log, "获取出售单数据");
		ObjectResult<LocalSale> jsonResult = new ObjectResult<LocalSale>();
		jsonResult.setFilter(LocalSaleFilter.getWithMemberAndLocalSaleGoodsesWithStoreGoods());
		LocalSale sale;
		try {
			sale = localSaleService.one(saleId);
			jsonResult.setResult(sale);
			jsonResult.setOk();
		} catch (Exception e) {
			error(log, e, "获取出售单异常");
			jsonResult.setErrorMessage("操作失败，请联系管理员");
		}
		
		return jsonResult;
	}
	
	@RequestMapping("/updateDo/json")
	@ResponseBody
	public StatusResult updateDoJson(LocalSale sale, ListObject listObj) {
		debug(log, "挂起销售单");
		StatusResult jsonResult = new StatusResult();
		try {
			localSaleService.editByStore(getStore(true), sale, listObj.getFsg());
			jsonResult.setOk();
		} catch (MyException e) {
			jsonResult.setFailMessage(e.getMessage());
		} catch (Exception e) {
			error(log, e, "挂起异常");
			jsonResult.setErrorMessage("挂起失败，请联系管理员");
		}
		return jsonResult;
	}
	
	@RequestMapping("/delDo/json")
	@ResponseBody
	public StatusResult delDoJson(long saleId) {
		debug(log, "取消（删除）出售单");
		StatusResult jsonResult = new StatusResult();
		try {
			localSaleService.removeByStore(getStore(true), saleId);
			jsonResult.setOk();
		} catch (Exception e) {
			error(log, e, "取消（删除）出售单异常");
			jsonResult.setErrorMessage("操作失败，请联系管理员");
		}
		return jsonResult;
	}
	
	@RequestMapping("/print")
	public String print(Model model) {
		debug(log, "进入销售单打印页面");
		
		model.addAttribute("store", getStore(true));
		return "shop/localSale_print_v1.1";
	}
	
	public static class ListObject {
		List<LocalSaleGoods> fsg;

		public List<LocalSaleGoods> getFsg() {
			return fsg;
		}

		public void setFsg(List<LocalSaleGoods> fsg) {
			this.fsg = fsg;
		}

	}
	
	public static class InitData {
		@JsonSerialize
		String localSetting;
		@JsonSerialize
		List<LocalSale> localSales;
		
		public List<LocalSale> getLocalSales() {
			return localSales;
		}
		public void setLocalSales(List<LocalSale> localSales) {
			this.localSales = localSales;
		}
		public String getLocalSetting() {
			return localSetting;
		}
		public void setLocalSetting(String localSetting) {
			this.localSetting = localSetting;
		}
	}
	
}
